package com.wc.AlgoOJ_homework.AlgoOJ_BL3.E_巧克力;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/4/4 10:02
 * @description http://43.138.190.70:8888/p/505?tid=6602a77318e03a818a4bf04e
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[][] a = new int[N][3];
    static PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
    static int x, n;

    // 从最后一天开始吃，但是还要吃价格最低的
    public static void main(String[] args) {
        x = sc.nextInt();
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 3; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        Arrays.sort(a, 1, n + 1, Comparator.comparing(o -> -o[1]));
        long res = 0;
        int idx = 1;
        while (x > 0) {
            while (idx <= n && a[idx][1] >= x) {
                q.add(a[idx]);
                idx++;
            }
            if (q.isEmpty()) {
                res = -1;
                break;
            } else {
                res += q.peek()[0];
                // 数量--
                q.peek()[2]--;
                if (q.peek()[2] == 0) q.poll();
                x--;
            }
        }
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
